//一个Socket对应一个玩家
import java.io.*;
import java.net.Socket;
public class Server_SocketToClient{
    Socket socket;
    DataInputStream in;
    DataOutputStream out;
    String user;
    String password;
    String sql;
    int isReady;    //是否准备,0没准备,1追备好了
    double num;     //每轮提交的数字
    int grade;      //对局的成绩
    Server_SocketToClient(Socket socket){
        isReady = 0;
        grade = 0;
        num = 0;
        this.socket = socket;
        try {
            in = new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
            new Server_SocketToClient_Receive(this).start();
        }catch(Exception e) {}
    }
    void register(){
        try {
            user = in.readUTF();
            password = in.readUTF();
        }catch(Exception e) {}
        sql = "select `账号` from `玩家信息` where `账号`='"+user+"'";
        if(Server_Database.queryOne(sql).equals("")) {
            sql = "insert into `玩家信息` values('"+user+"','"+password+"',0,0,0)";
            Server_Database.update(sql);
            sql = "create table `"+user+"-战绩`"+
                    "(`日期` date,`时间` time,`场级` char(6),"+
                    "`人数` int,`排名` int,"+
                    "primary key(`日期`,时间))";
            Server_Database.largeUpdata(sql);
            try {
                out.writeUTF("注册成功");
            }catch(Exception e) {}
        }else {
            try {
                out.writeUTF("此账号已存在");
            }catch(Exception e) {}    
        }
    }
    void login() {
        try {
            user = in.readUTF();
            password = in.readUTF();
        }catch(Exception e) {}
        sql = "select `账号` from `玩家信息` where `账号`='"+user+"'";
        if(Server_Database.queryOne(sql).equals("")) {
            try {
                out.writeUTF("此用户不存在");
            }catch(Exception e) {}    
        }else {
            sql = "select `密码` from `玩家信息` where `账号`='"+user+"'";
            if(Server_Database.queryOne(sql).equals(password)) {
                try {
                    out.writeUTF("登陆成功");
                }catch(Exception e) {}           
            }else {
                try {
                    out.writeUTF("密码错误");
                }catch(Exception e) {}
            }
        }
    }
    void query() {
        String data = Server_Database.query1("select * from `"+user+"-战绩`");
        String data1 = Server_Database.query1("select `总场数`,`总分数`,`赢数` from `玩家信息` where `账号`='"+user+"'");
        try {
            out.writeUTF(data);
            out.writeUTF(data1);
        }catch(Exception e) {}
    }
}